<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="form" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="寄件人名称">
          <el-input
            v-model="queryParams.keywords"
            placeholder="寄件人名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" icon="el-icon-search">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <!-- <el-button type="success" @click="openDialog()"
          ><i class="el-icon-circle-plus"></i> 新增</el-button> -->
        <el-button
          type="danger"
          :disabled="ids.length === 0"
          @click="handleDelete()"
          ><i class="el-icon-delete"></i> 删除</el-button>
      </template>

      <el-table v-loading="loading" :data="dataList" highlight-current-row border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="发件人名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="发件手机号">
                <span>{{ props.row.phone }}</span>
              </el-form-item>
              <el-form-item label="发件地址">
                <span>{{ props.row.addr }}</span>
              </el-form-item>
              
              <el-form-item label="收件人名称">
                <span>{{ props.row.receiveName }}</span>
              </el-form-item>
              <el-form-item label="收件手机号">
                <span>{{ props.row.receivePhone }}</span>
              </el-form-item>
              <el-form-item label="收件地址">
                <span>{{ props.row.receiveAddr }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="序号" width="55" prop="dataIndex"/>
        <el-table-column label="发件人" prop="name"/>
        <el-table-column label="收件人" prop="receiveName"/>
        <el-table-column label="运单号" prop="number"/>
        <el-table-column label="关联样本" prop="sampleNumber"/>
        <el-table-column label="关联vip卡" prop="vipCardNumber"/>
        <el-table-column label="更新时间" prop="updateTimeText"/>
        <el-table-column fixed="right" label="操作"  min-width="340" width="340">
          <template #default="scope">
            <el-button type="primary" size="small" link :disabled="scope.row.number == null" @click="openExpressDialog(scope.row.id)"><i class="el-icon-view"></i> 查看物流信息</el-button>
            <el-button type="primary" size="small" link :disabled="scope.row.number == null" @click="openPayWillsDialog(scope.row)"><i class="el-icon-view"></i> 电子面单</el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row.id)"><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog
      :visible.sync="dialog.visible"
      :title="dialog.title"
      width="800px"
      :close-on-click-modal="false"
      :before-close="closeDialog">
      <el-table
        v-loading="loading"
        :data="expressRouterList"
        highlight-current-row
        border
      >
        <el-table-column label="地址" prop="acceptAddress"/>
        <el-table-column label="时间" prop="acceptTime"/>
        <el-table-column label="备注" prop="remark"/>
      </el-table>
    </el-dialog>

    <el-dialog
      :visible.sync="payWillsDialog.visible"
      :title="payWillsDialog.title"
      width="800px"
      :close-on-click-modal="false"
      :before-close="closePayWillsDialog">
      <iframe ref="pdfCotainer" :src="payWillsUrl" width="100%" height="650px"></iframe>
    </el-dialog>

    <!-- 字典表单弹窗 -->
    <!-- <el-dialog
      :visible.sync="dialog.visible"
      :title="dialog.title"
      width="500px"
      :before-close="closeDialog"
    >
      <el-form
        ref="userFormRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="KEY" prop="name">
          <el-input v-model="formData.name" placeholder="请输入KEY" />
        </el-form-item>
        <el-form-item label="二级KEY" prop="secondName">
          <el-input v-model="formData.secondName" placeholder="请输入二级KEY" />
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="formData.value" placeholder="请输入值" />
        </el-form-item>
        <el-form-item label="描述" prop="describe">
          <el-input v-model="formData.describe" type="textarea" :autosize="{ minRows: 8}" placeholder="请输入描述" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="addOrUpdate">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog> -->
  </div>
</template>

<script>

import expressApi from '@/api/aliyun/detection/express'
import { formatDate } from '@/utils/formatTime'
export default {
	name: 'dictIndex',
	data() {
		return {
			queryParams: {
        keywords: '',
        pageNum: 0,
        pageSize: 10,

      },
      total: 0,
      loading: false,
      dataList: [],
      ids: [],
      dialog: {},
      formData: {},
      rules: {},

      sampleId: '',
      expressRouterList: [],

      payWillsDialog: {},
      payWillsUrl: '',
		};
	},
  methods: {
    openPayWillsDialog (row) {
      this.$set(this.payWillsDialog, 'visible', true);
      this.$set(this.payWillsDialog, 'title', '查看电子面单');
      expressApi.downloadWills(row.id).then(res => {
        const binaryData = [];
        binaryData.push(res);
        this.payWillsUrl = window.URL.createObjectURL(new Blob(binaryData, {type: 'application/pdf'}));
      })
    },
    closePayWillsDialog () {
      this.$set(this.payWillsDialog, 'visible', false);
    },
    openExpressDialog (id) {
      this.$set(this.dialog, 'visible', true);
      this.$set(this.dialog, 'title', '快递信息');
      this.loading = true;
      expressApi.routers(id).then(res => {
        this.expressRouterList = res.data.routeDtos;
        this.loading = false;
      })
    },
    handleQuery() {
      this.loading = true;
      let params = {
        number: this.queryParams.pageNum,
        size: this.queryParams.pageSize
      };
      expressApi.page(params).then((res) => {
        let data = res.data;
        let that = this;
        let index = 1
        data.content.forEach((v) => {
          v.createTimeText = formatDate(new Date(v.createTime), "YYYY-mm-dd HH:MM:SS")
          v.updateTimeText = formatDate(new Date(v.updateTime), "YYYY-mm-dd HH:MM:SS")
          v.dataIndex = index++;
        });
        this.dataList = data.content;
        
        this.total = data.count;
        this.loading = false;
      }).catch(() => {});
    },
    resetQuery() {
      this.queryParams = {
        keywords: '',
        pageNum: 0,
        pageSize: 10,
      };
      this.handleQuery();
    },
    
    // addOrUpdate() {
    //   let that = this;
    //   if (this.formData.id) {
    //     dictApi.update(this.formData).then((res) => {
    //       this.$message({message: '字典数据更新成功！', type: 'success'});
    //       that.closeDialog();
    //     });
    //   } else {
    //     dictApi.add(this.formData).then((res) => {
    //       this.$message({message: '字典数据添加成功！', type: 'success'});
    //       that.closeDialog();
    //     });
    //   }
    // },
    // openDialog(id) {
    //   this.$set(this.dialog, 'visible', true);
    //   let that = this;
    //   if (id) {
    //     this.$set(this.dialog, 'title', '编辑字典数据');
    //     dictApi.get(id).then((res) => {
    //       let data = res.data;
    //       that.formData = data;

    //     });
    //   } else {
    //     this.$set(this.dialog, 'title', '新增字典数据');
    //     that.formData = {};
    //   }

    // },
    closeDialog() {
      this.$set(this.dialog, 'visible', false);
      this.$set(this.dialog, 'title', '');
    },
    handleDelete(id) {
      if (id) {
        expressApi.delete(id).then((res) => {
          this.$message({message: '快递信息删除成功！', type: 'success'});
          this.handleQuery();
        });
      } else {
        this.$message.error('暂不支持批量删除快递信息！');
      }
    },
    handleSelectionChange(val) {
      this.ids = [];
      val.forEach(v => {
        this.ids.push(v.id);
      });
    },
    handleSelectionRoleChange(rows) {
      if (rows) {
        let arr = [];
        rows.forEach((v) => {
          arr.push(v.id);
        });
        this.selectRoleIdArr = arr;
      }
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.handleQuery();
    },
  },
  created() {
    this.handleQuery();
  }
};
</script>

<style scoped>
.el-pagination {
  margin-top: 10px;
  float: right;
  margin-bottom: 10px;
}
.demo-table-expand {
  font-size: 0;
}
/deep/.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
  margin-left: 20px;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}
</style>